/*
	Content scripts是在Web页面内运行的javascript脚本
*/
//(function(){

//var WEB_PATH = 'http://localhost/springmvc';
var WEB_PATH = 'https://hm1801.duckdns.org:60080/springmvc';

function _onPageKeydown_(){
	var e = window.event;
	if(e && e.keyCode==192){
		window.open('', '_self', ''); 
		window.close(); 
	}
}

document.body.addEventListener('keydown',_onPageKeydown_,false); 

//===============================

var old_border = '';
var reset_border = false;

function _onPageMouseover_(){
	var e = window.event;
	var element = e.target;
	if(e.ctrlKey && e.altKey && !isSelected()){
		old_border = element.style.border;
		reset_border = true;
		element.style.border = '1px solid #000'
	}
}

function _onPageMouseout_(){
	var e = window.event;
	var element = e.target;
	if(reset_border){
		element.style.border = old_border;
	}
}

function _onPageMousedown_(){
	handleSelection();
	pickImgs();
}

document.body.addEventListener('mouseover',_onPageMouseover_,false); 
document.body.addEventListener('mouseout',_onPageMouseout_,false); 
document.body.addEventListener('mousedown',_onPageMousedown_,false); 

//============================================
function pickImgs(){
	var event = window.event;
	var element = event.target;
	if(!(event.ctrlKey &&event.shiftKey) || event.button!=0){
		return;
	}

	var imgList = DomUtil.imgsFromPoint(event.clientX, event.clientY);
	var srcList = [];
	var descList = [];
	var reg = /url\(["']?(.*)['"]?\)/;
	for(var i=0;i<imgList.length;i++){
		var img = imgList[i];
		var imgsrc = '';
		var imgDesc = ' ';
		if(img.type=='img' && img.element.src){
			imgsrc = img.element.src;
			imgDesc = img.element.title||img.element.alt;
		}else if(img.type=='bg' && img.bg && img.bg!='none'){
			imgsrc = img.bg.replace(reg, '$1');
		}else{
			continue;
		}
		if(img.element==element || img.type=='img'){
			srcList.unshift(imgsrc);
			descList.unshift(imgDesc);
		}else{
			srcList.push(imgsrc);
			descList.push(imgDesc);
		}
	}

	var doc_href = window.location.href;
	var doc_title = document.title;

	var params = {doc_href:doc_href, doc_title:doc_title, imgs:srcList.join('#'), titles:descList.join('#'),  cookie:document.cookie};
	var url = WEB_PATH+'/photo/pick.xhtml';
	makeForm('_select_imgs_', url, params);
	submitData('_select_imgs_', params);
}

function handleSelection(){
	var e = window.event;
	var element = e.target;
	if(!(e.ctrlKey &&e.altKey) || event.button!=0){
		return;
	}
	var top_href = top.location.href;
	var top_title = top.document.title;
	var href = window.location.href;
	var doc_title = document.title;
	var text = null;
	var html = getSelectedHtml();
	if(!html){
		html = element.outerHTML;
		text = element.innerText;
	}else{
		text = window.getSelection().toString().replace(/(^\s*)|(\s*$)/g, "");
	}
	var params = {title:doc_title, text:text, html:html, href:href, top_href:top_href, top_title:top_title,  cookie:document.cookie};
	var url = WEB_PATH+'/chapter/pick.xhtml';
	makeForm('_select_html_', url, params);
	submitData('_select_html_', params);
	
	/*
	var urlParams = 'text='+encodeURIComponent(text)+'&doc_title='+encodeURIComponent(doc_title)+'&href='+encodeURIComponent(href)+'&top_href='+encodeURIComponent(top_href)+'&top_title='+encodeURIComponent(top_title);
	var url = WEB_PATH+'/chapter/pick.xhtml?' + urlParams;
	window.open(url,'editWin');
	*/
}

function isSelected(){
	var rng = window.getSelection();
	return rng.rangeCount > 0 && rng.toString();
}

function getSelectedHtml(){
	var html = '';
	var rng = window.getSelection();
	if (rng.rangeCount > 0 && window.XMLSerializer) {
		rng = rng.getRangeAt(0);
		var documentFragment = rng.cloneContents();
		cleanNodes(documentFragment);
		html = new XMLSerializer().serializeToString(documentFragment);
	}
	return html;
}

function cleanNodes(fragment){
	if (!fragment.hasChildNodes()) {
		return ;
	}
	var nodeList = fragment.childNodes;
	for (var i = 0; i < nodeList.length; i++) {
		var node = nodeList[i];
		var tagName = node.tagName;
		if(tagName){
			tagName = node.tagName.toLowerCase();
			if(tagName=='script'){
				fragment.removeChild(node);
			}else{
				cleanNodes(node);
			}
		}
	}
}

function makeForm(formId, action, params){
	var form = document.getElementById(formId);
	if(form){
		return;
	}
	var form_div = document.createElement('div');
	form_div.setAttribute('style', 'display:none');
	document.body.appendChild(form_div);
	form = document.createElement('form');
	form_div.appendChild(form);
	form.setAttribute('id', formId);
	form.setAttribute('action', action);
	form.setAttribute('method', 'post');
	form.setAttribute('target', formId+'_edit_form_');
	form.setAttribute('accept-charset', 'utf-8');
	for(var name in params){
		var val = params[name];
		var input = document.createElement('input');
		input.setAttribute('name', name);
		form.appendChild(input);
	}
}

function submitData(formId, params){
	var form = document.getElementById(formId);
	for(var name in params){
		var val = params[name];
		var input = form[name];
		input.value = val;
	}
	form.submit();
}

//})();